package com.ebook.ui.activity;

import com.ebook.ui.fragment.Fragment;
import com.ebook.ui.fragment.home.category.CategoryFragment;
import com.ebook.ui.fragment.home.everyrecommend.EveryEliteChooseFragment;
import com.ebook.ui.fragment.home.shuffle.ShufflingFragment;

import javax.swing.*;
import java.awt.*;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Hashtable;

public class Activity extends JPanel {
    private Status status = Status.preInit;
    private Hashtable<Class<?>,Fragment> fragments;
    public Activity() throws NoSuchFieldException, IllegalAccessException {
        super();
        setLayout(null);
        setBackground(Color.white);
    }
    public void initialize(){

    }
    public void loadActivity() throws NoSuchFieldException, IllegalAccessException {
        fragments = new Hashtable<>();
        if(status!=Status.preInit){
            return;
        };
    }
    public void exitActivity(){

    }
    public void destroyActivity(){

    }
    public void loadFragmentFromClasses(ArrayList<Class<?>> clazz){
        for(Class<?> cla:clazz){
            Fragment fragment = (Fragment) getObject(cla);
            fragments.put(cla,fragment);
            add(fragment);
        }
    }
    public Fragment getFragmentByDescribe(String describe){
        for(Fragment fragment:fragments.values()){
            if(fragment.describe.equals(describe)){
                return fragment;
            }
        }
        return null;
    }
    /* 未优化的代码,需要占用较多的内存空间
    **/
    public Fragment getFragmentByClass(Class<?> clazz) throws NoSuchFieldException, IllegalAccessException {
        return fragments.get(clazz);
    }
    private <T> T getObject(Class<T> clazz){
        T instance = null;
        try{
            instance = (T)clazz.getDeclaredConstructor().newInstance();
        } catch (InvocationTargetException | InstantiationException
                | NoSuchMethodException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return instance;
    }
    @Deprecated
    private void loadFragmentsFromPackage(String fragmentPackage){
        Class<?> clazz = Fragment.class;
        Package pks = clazz.getPackage();
        System.out.println(pks);
    }
    @Deprecated
    private void loadFragmentsFromPackage(Package fragmentPackage){

    }
    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        Activity activity = new HomeActivity();
        ArrayList<Class<?>> clazz = new ArrayList<>();
        clazz.add(CategoryFragment.class);
        clazz.add(EveryEliteChooseFragment.class);
        clazz.add(ShufflingFragment.class);
        activity.loadFragmentFromClasses(clazz);
        System.out.println(activity.getFragmentByClass(CategoryFragment.class));
    }

    @Override
    public String toString() {
        return "Activity{" +
                "status=" + status +
                ", fragments=" + fragments +
                '}';
    }


}
